package remato.client.view.util;


import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

/**
 * 
 * @author Petter L. H. Eide
 * @version $Id: ViewerAction.java,v 1.1 2006-01-23 19:58:37 petterei Exp $
 * 
 * Changelog:
 * 
 * $Log: ViewerAction.java,v $
 * Revision 1.1  2006-01-23 19:58:37  petterei
 * initial commit for client, fitting the alternative session implementation
 *
 * Revision 1.3  2005-12-14 20:27:21  petterei
 * added custom images
 *
 * Revision 1.2  2005-12-13 12:10:54  petterei
 * refactorin + bugfix
 *
 * Revision 1.1  2005/09/27 17:04:29  petterei
 * inittial commit
 *
 * Revision 1.3  2005/08/23 06:40:47  peide3
 * *** empty log message ***
 *
 * Revision 1.2  2005/08/22 16:27:42  petterei
 * *** empty log message ***
 *
 * Revision 1.1  2005/07/31 22:55:10  petterei
 * initial commit
 *
 * Revision 1.2  2005/07/20 08:48:40  peide3
 * updated and implemented more functionality
 *
 * Revision 1.1  2005/07/20 06:06:39  peide3
 * implemented Eclipse-IActions
 *
 *
 */
public class ViewerAction extends Action {
    
    private String message;
    private StructuredViewer viewer;
    private boolean displayMessage = false;

    public ViewerAction(String text, ImageDescriptor image) {
		super(text, image);
	}
    public ViewerAction(String text) {
      this(text, PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
	}

	/**
     * @see org.eclipse.jface.action.Action#run()
     */
    @Override
    public void run() {
        displayMessage();
    }

    public void setDisplayMessage(String message, StructuredViewer viewer) {
        this.message = message;
        this.viewer = viewer;
    }

    private void displayMessage() {
        if (viewer != null && displayMessage) {
            MessageDialog.openInformation(
                    viewer.getControl().getShell(), "Action performed: " + getText(), "Successfully: " + message
                    );
        }
    }

    protected String getMessage() {
        return message;
    }

    protected void setMessage(String message) {
        this.message = message;
    }

    public void setViewer(StructuredViewer viewer) {
        this.viewer = viewer;
//        if (message != null ) {
//            message = "The action was performed."; 
//        }
        if (message == null ) {
            message = "The action was performed."; 
        }
    }

    public boolean willMessageBeDisplayed() {
        return displayMessage;
    }

    public void showDisplayMessage(boolean displayMessage) {
        this.displayMessage = displayMessage;
    }

    protected StructuredViewer getViewer() {
        return viewer;
    }
}
